<?php

class to_parent extends Controller {

	function to_parent()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
	    $data['to_parent_img'] = $this->filemanagementmodel->getByPostion('to_parent');
		
		$this->load->view('f_header', $data);
		$this->load->view('f_to_parent', $data);
		$this->load->view('f_footer', $data);
	}
	
	function edit()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
		$this->load->model('filemanagementmodel');
	    $data['to_parent_img'] = $this->filemanagementmodel->getByPostion('to_parent');
		
		$this->load->view('header');
		$this->load->view('f_to_parent_edit', $data);
		$this->load->view('footer');
	}
	
	function upload()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
	    $this->load->model('filemanagementmodel');
		$msg = array('msg' => '');
		
	   
	     if(isset($_FILES))
        {
        	$this->load->library('upload');
        	if(@$_FILES['txtFiles']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png|';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'parent1';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('txtFiles'))
				{
					$msg['msg'] = "ไม่สามารถ upload ไฟล์รูปภาพด้านซ้ายได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'to_parent',
		               'file_name' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	if(@$_FILES['txtFiles2']['error'] == '0') 
        	{
        		
				$config['upload_path'] = './data-image/file_management/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = 'parent2';
				$this->upload->initialize($config);
				
				
				if ( ! $this->upload->do_upload('txtFiles2'))
				{
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปภาพด้านขวาได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'position' => 'to_parent',
		               'detail' => $data['upload_data']['file_name']   
		            );
					$this->filemanagementmodel->update($Imgdata);
				}
        	}
        	if($msg['msg'] == '') 
        	{
        		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        	} 
        } else {
        	$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        }
		
		$this->load->model('filemanagementmodel');
	    $data['to_parent_img'] = $this->filemanagementmodel->getByPostion('to_parent');
		
		$this->load->view('header');
		$this->load->view('f_to_parent_edit', $data);
		$this->load->view('footer', $msg);
	}
}

?>